#!/usr/bin/python
# -*-coding:utf-8-*-
# pip install ddddocr 图片识别
"""菜篮子1"""
import math
import time
import re
import requests
import json
from ast import literal_eval
from bs4 import BeautifulSoup
from time import sleep
import common
import db
from datetime import datetime
from dotenv import load_dotenv, find_dotenv
import os
import asyncio
from pyppeteer import launch
import os

mysqldb = db.DbManager()

load_dotenv(verbose=True)
# 今天
today_time = common.TodayTime()
# 将-替换为.
today_time_new = today_time.replace('-', '.')
# 账号
username = os.getenv('LOGIN_ADMIN')
passworld = os.getenv('LOGIN_PWD')

# 获取登录后cookie
async def get_cookie(page):
    cookies_list = await page.cookies()
    cookies = ''
    for cookie in cookies_list:
        str_cookie = '{0}={1};'
        str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
        cookies += str_cookie
    print(cookies)
    return cookies


async def login():
    executablePath = '/Users/wankaifa/Library/Application Support/pyppeteer/local-chromium/588429/chrome-mac/Chromium.app/Contents/MacOS/Chromium'

    # 查询文件是否存在 url
    with open(os.getcwd()+"/link.txt", 'r+') as file:
        a = file.read().splitlines()
        # file.seek(0)  # 定位
        # 清空文件内容
        # file.truncate()
    print(len(a))
    if len(a)==0:
        exit()
    for link in range(0,len(a)):
        print(a[link])
        # http://nync.lyg.gov.cn//lygnyxxw/csdt/content/fb312da8-7759-4cbc-ae83-acfc8ef496dd.html
        log_url = "http://nync.lyg.gov.cn/"+a[link]
        headers = {
            'Host':'nync.lyg.gov.cn',
            'referer': 'http://nync.lyg.gov.cn/',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_16_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3542.0 Safari/537.36',
        }
        resp = requests.get(
            log_url,
            headers=headers)
        resp.encoding=resp.apparent_encoding
        soup = BeautifulSoup(resp.text, "html.parser")
        # 标题
        # print(resp.text)
        title = soup.find("h2").text
        # 发布日期
        date_str = soup.select("body > div > div:nth-child(4) > div > div.article > h3 > span:nth-child(1)")[0]
        new_date_str=date_str.text+":00"
        article_list = soup.find("div", class_='article_list')
        # 获取本期月份
        article_p2=article_list.select("p:nth-child(2)")
        # 需要的正文内容
        article_p2_text=article_p2[0].text
        if "本期平均交易价" not in article_p2_text:
            # body > div > div:nth-child(4) > div > div.article > div > p:nth-child(4)
            article_p2 = article_list.select("p:nth-child(4)")
            article_p2_text = article_p2[0].text
        import re
        # 本期平均交易价
        price_arr_1=re.split("本期平均交易价", article_p2_text)
        print(price_arr_1)
        price_arr_1_2=re.split(",",price_arr_1[1])
        price_1="本期平均交易价"+price_arr_1_2[0]
        month_arr=re.split("，",article_p2_text)
        month_arr_1=re.split("。",month_arr[1])
        title_str=month_arr_1[0]
        man_str=title_str+price_1
        # pat = r'\d+\.?\d+'
        pat = r'\d+(?:\.\d*)?'
        v1 = re.findall(pat, man_str)
        # 获取table html
        article_table = soup.find("table")
        # 创建字典
        product_list=[]
        product_price=[]
        new_product = []
        for k in range(0,len(article_list.findAll("tr"))):
            if k >0:
                new_tr=article_list.findAll("tr")[k]
                name=""
                price=""
                for td_k in range(0,len(new_tr.findAll("td"))):
                    print(new_tr.findAll("td")[td_k])
                    if td_k % 2==0:
                        # 偶数
                        name=new_tr.findAll("td")[td_k].text
                        name = name.replace(" ","")
                        name = name.strip('\u3000')
                        if "\u3000" in name:
                            name = name.replace("\u3000", "")
                        product_list.append(name)
                    else:
                        # 奇数
                        price = new_tr.findAll("td")[td_k].text
                        price = price.replace(" ", "")
                        price = price.strip('\u3000')
                        if "\u3000" in price:
                            price = price.replace("\u3000", "")
                        if price=='——':
                            price=0
                        product_price.append(price)
        new_product=[]
        for ke in range(0,len(product_list)):
            arr = {
                'name': product_list[ke],
                'price': product_price[ke]
            }
            new_product.append(arr)
        info = {"title": title,
                "content": article_list,
                'part_list': json.dumps(v1, ensure_ascii=False),
                'part_list_html': title_str + price_1,
                'product_table_html':article_table,
                'product_list':product_list,
                'product_price_list':product_price,
                'product_list_json':json.dumps(new_product, ensure_ascii=False),
                'publishDate': new_date_str,
                'link':log_url,
                }
        now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        find_info = mysqldb.table_select_one(table="agri_price_clz", field="id,publishDate",
                                             where={"title": title})
        # 存在数据则更新
        if find_info:
            info['update_date'] = now_time
            mysqldb.table_update(table="agri_price_clz", data=info,
                                 where={"title": title})
        else:
            info['create_date'] = now_time
            up_info = mysqldb.table_select_one(table="agri_price_clz", field="id,product_list,product_price_list",
                                               where="1=1", order="publishDate", sort="desc")
            if up_info!=None:
                # 不想等这人工计算 环比
                up_product_list = up_info['product_list']
                up_product_price = literal_eval(up_info['product_price_list'])
                price_ratio = []
                if len(up_product_list) == len(str(product_list)):
                    for key in range(0, len(product_list)):
                        # 上期商品价格
                        json_name = product_list[key]
                        up_price = up_product_price[key]
                        # 本期商品价格
                        new_product_price = product_price[key]
                        if float(up_price) != 0.0:
                            json_product_ratio = round((float(new_product_price) - float(up_price)) / float(up_price) * 100,
                                                       2)
                        else:
                            json_product_ratio = 0
                        new_list = {
                            'name':json_name,
                            'price':json_product_ratio
                        }
                        price_ratio.append(new_list)
                info['price_ratio'] = json.dumps(price_ratio, ensure_ascii=False)
            mysqldb.table_insert(table="agri_price_clz", data=info)





class Egas:
    def __init__(self):
        self.url = "http://zs.jsncpaq.com/"
        self.main()

    def main(self):
        # 登录
        # 菜篮子 2022 第三期和第二期没有
        asyncio.get_event_loop().run_until_complete(login())

Egas()
